﻿namespace PugLib
{
	using System;
	using System.Threading;

	/// <summary>
	/// A windows forms friendly implementation of an asynchronous dispatcher.
	/// </summary>
	public class Dispatcher : IDispatcher
	{
		private readonly SynchronizationContext _context;

		public Dispatcher(SynchronizationContext context)
		{
			_context = context;
		}

		#region IDispatcher Members

		public void Post(Action action)
		{
			_context.Post(state => action(), null);
		}

		#endregion

		/// <summary>
		/// Creates a default dispatcher based on the calling thread's context.
		/// </summary>
		/// <returns></returns>
		public static Dispatcher CreateDefault()
		{
			return new Dispatcher(SynchronizationContext.Current);
		}
	}
}